/*
package org.itboys.mobile.service.mysql.mobile;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.TianJieGroupRecordMapper;
import org.itboys.mobile.dao.TianJieWalletProductMapper;
import org.itboys.mobile.dao.TianJieWalletRecordMapper;
import org.itboys.mobile.dto.TianJieWalletDto;
import org.itboys.mobile.dto.TianJieWalletProductDto;
import org.itboys.mobile.dto.TianJieWalletRecordDto;
import org.itboys.mobile.entity.mongo.TianJieMember;
import org.itboys.mobile.entity.mysql.*;
import org.itboys.mobile.service.mongo.mobile.TianJieMobileMemberService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

*/
/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：天街_消费卡_service
 *//*

@Service
public class TianJieMobileWalletServiceOld extends BaseMySqlService<TianJieWalletOld> {
    @Autowired
    private TianJieWalletRecordMapper walletRecordMapper;
    @Autowired
    private TianJieWalletProductMapper walletProductMapper;
    @Autowired
    private TianJieMobileMemberService memberService;
    @Autowired
    private TianJieMobileWalletBlockService walletBlockService;
    @Autowired
    private TianJieMobileGroupService groupService;
    @Autowired
    private TianJieGroupRecordMapper groupRecordMapper ;
    */
/**
     * 查找用户消费卡主体
     * @param memberId

     * @return
     *//*

    @Transactional
    public TianJieWalletDto getWallet(Long memberId,Boolean isCheckRevenue){
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("memberId", memberId);
        TianJieWalletOld wallet=null;
        List<TianJieWalletOld> wallets=super.select(param);
        if(ListUtil.isNotNull(wallets)){
            wallet=wallets.get(0);
        }else {
            wallet=new TianJieWalletOld(memberId);
            super.insert(wallet);
            wallet=super.find(wallet.getId());
        }
        return packageDtoSingle(wallet,isCheckRevenue);
    }

    */
/**
     * 通过用户寻找消费卡钱包
     * @param memberId
     * @return
     *//*

    @Transactional
    public TianJieWalletOld findOne(Long memberId ){
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("memberId", memberId);
        TianJieWalletOld wallet=null;
        List<TianJieWalletOld> wallets=super.select(param);
        if(ListUtil.isNotNull(wallets)){
            wallet=wallets.get(0);
        }else {
            wallet=new TianJieWalletOld(memberId);
            super.insert(wallet);
            wallet=super.find(wallet.getId());
        }
        return wallet;
    }
    */
/**
     * 获取消费卡（钱包消费记录）
     *//*

    public List<TianJieWalletRecordDto> getRecord(HttpServletRequest request,Long memberId){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"");
        param.put("status", SundayCommonConstants.YES);
        param.put("memberId",memberId);
     //   List<TianJieWalletRecord> records=
        return packageRecordDtoMuilt(walletRecordMapper.select(param));
    }

    */
/**
     *
     *//*

    */
/**
     * 拨付。团队领导人将自己的消费卡余额，驳回给团队成员
     * @param memberId
     * @param partnerId
     * @param amount
     *//*

    @Transactional
    public void present(Long memberId,Long partnerId,BigDecimal amount){
        //校验是否是队长
        TianJieMember member = memberService.getById(memberId);
        if(member.getFinanceIsCaptain()!=SundayCommonConstants.YES){
            throw new ErrorException("您不是队长，无法拨付");
        }

        //获取团队可用金额
        TianJieGroup group =groupService.find(member.getFinanceGroupId());
        if(amount.compareTo(group.getPresentAmount())==1){
            throw new ErrorException("团队可用拨付金额不足，请重试");
        }
        //扣减团队的可用拨付金额
        group.setPresentAmount(group.getPresentAmount().subtract(amount));
        groupService.update(group);
        //生成团队拨付记录
        TianJieMember partner = memberService.getById(partnerId);
        groupRecordMapper.insert( new TianJieGroupRecord(group.getId(), partnerId, partner.getNickName(),  amount,2));
        //增加队员的可用金额
        generateRecord(null,partnerId, amount,SundayCommonConstants.wallet_record_type_present,"团队拨付",0l);
      //  generateRecord(wallet,partnerId, amount,SundayCommonConstants.wallet_record_type_present_receive,"来至团队的拨付",0l);
    }

    */
/**
     * 托管到区块链。
     * @param memberId
     * @param amount
     *//*

    public void deposit(Long memberId,BigDecimal amount){
        TianJieWalletOld wallet= findOne(memberId);
        BigDecimal unreleaseAmount =wallet.getUnreleaseAmount();
        if(amount.compareTo(unreleaseAmount)==1){
            throw new ErrorException("消费卡待释放余额不足，请重试");
        }
        generateRecord(wallet,memberId, amount,SundayCommonConstants.wallet_record_type_deposit,"托管到区块链",0l);
    }


    */
/**
     * 获取充值产品
     *//*

    public List<TianJieWalletProductDto> getProducts(Integer type){
        Map<String,Object> param =new HashMap<String,Object>();
        param.put("type",type);
        return packageProductDtoMuilt(walletProductMapper.select(param));
    }

    */
/**
     * 生成消费卡记录
     * @param memberId
     * @param amount
     * @param type
     *//*

    @Transactional
    public Long generateRecord(TianJieWalletOld wallet, Long memberId, BigDecimal amount, Integer type, String remark, Long recordId){

             if(wallet==null){
                 wallet=findOne(memberId);
             }

             boolean isChangeWallet=true;//是否修改钱包
             boolean isGenerateReleaseRecord=false;//是否生成释放计划
             BigDecimal negativeOne=new BigDecimal("-1");
            //本次变动钱包总额
             BigDecimal thisTotalAmount =new BigDecimal("0");
            //本次变动钱包待释放金额
            BigDecimal thisUnreleaseAmount =new BigDecimal("0");
            //本次变动钱包已释放金额
            BigDecimal thisReleaseAmount =new BigDecimal("0");
            //本次变动钱包已释放金额
            BigDecimal thisAbleReleaseAmount =new BigDecimal("0");
            //本次冬季金额
            BigDecimal thisFrozenAmount = new BigDecimal("0");

             //充值，生成未支付的充值记录，不变
            if(type==SundayCommonConstants.wallet_record_type_charge){
                TianJieWalletRecord walletRecord=new TianJieWalletRecord( memberId,  wallet.getId(), type, remark, wallet.getUnreleaseAmount(),amount, SundayCommonConstants.NO);
                    walletRecordMapper.insert(walletRecord);
                    recordId=walletRecord.getId();
                    isChangeWallet=false;
                    isGenerateReleaseRecord=false;
            //消费卡充值后回掉.加
            }else if(type==SundayCommonConstants.wallet_record_type_charge_callback){
                TianJieWalletRecord walletRecord=new TianJieWalletRecord();
                walletRecord.setId(recordId);
                walletRecord.setRemark(remark);
                walletRecord.setStatus(SundayCommonConstants.YES);
                walletRecordMapper.update(walletRecord);
                //变更钱包主体信息
                thisTotalAmount=amount;
                thisUnreleaseAmount=amount;
                //是否生成释放计划
                isGenerateReleaseRecord=true;
                //充值之后，初始化一些金融用户属性
                TianJieMember member=memberService.getById(memberId);
                member.setId(memberId);
                if(member.getFinanceIsFinance()==null){
                    member.setFinanceIsCaptain(0);
                    member.setFinanceIsFinance(1);
                    member.setFinanceGroupId(0l);
                    memberService.updateExceptEmpty(memberId,member);
                }

            //游戏充值 减
            }else if(type==SundayCommonConstants.wallet_record_type_game){
                amount=amount.multiply(negativeOne);
                TianJieWalletRecord walletRecord=new TianJieWalletRecord( memberId,  wallet.getId(), type, remark, wallet.getUnreleaseAmount(),amount, SundayCommonConstants.YES);
                walletRecordMapper.insert(walletRecord);
                //变更钱包主体信息
                //thisTotalAmount=amount;
                thisUnreleaseAmount=amount;
                //是否生成释放计划
                isGenerateReleaseRecord=true;

             //托管区块链
            }else if(type==SundayCommonConstants.wallet_record_type_deposit){
                amount = amount.multiply(negativeOne);
                TianJieWalletRecord walletRecord=new TianJieWalletRecord( memberId,  wallet.getId(), type, remark, wallet.getUnreleaseAmount(),amount, SundayCommonConstants.YES);
                walletRecordMapper.insert(walletRecord);
                //变更钱包主体
                //减未释放
                thisUnreleaseAmount=amount;
                //加冻结
                amount =  amount.multiply(negativeOne);
                thisFrozenAmount=amount;
                //增加区块链卡钱包的信息
                walletBlockService.generateRecord(null,memberId,amount,null,null,SundayCommonConstants.block_record_type_in,remark,null);
            //队长将团队金额拨付给队员。减
            }else if(type==SundayCommonConstants.wallet_record_type_present){
                amount = amount.multiply(negativeOne);
                TianJieWalletRecord walletRecord=new TianJieWalletRecord( memberId,  wallet.getId(), SundayCommonConstants.wallet_record_type_present, remark, wallet.getUnreleaseAmount(),amount, SundayCommonConstants.YES);
                walletRecordMapper.insert(walletRecord);
                //变更钱包主体
                //加主金额
                thisTotalAmount=amount;
                //加未释放
                thisUnreleaseAmount=amount;
               //是否生成释放计划
                isGenerateReleaseRecord=true;


            }



            //生成释放计划。4个月释放全部
            if(isGenerateReleaseRecord){


            }
            //修改消费卡主体
            if(isChangeWallet){
                wallet.setTotalAmount(wallet.getTotalAmount().add(thisTotalAmount));
                wallet.setUnreleaseAmount(wallet.getUnreleaseAmount().add(thisUnreleaseAmount));
                wallet.setReleaseAmount(wallet.getReleaseAmount().add(thisReleaseAmount));
                wallet.setAbleReleaseAmount(wallet.getAbleReleaseAmount().add(thisAbleReleaseAmount));
                wallet.setFrozenAmount(wallet.getFrozenAmount().add(thisFrozenAmount));
                super.update(wallet);

            }
          //  super.update(wallet);
            //更改用户的信息

            return recordId;

    }



















    */
/**
     * 组装DTO，
     * @param wallet
     * @param isCheckRevenue 是否查询实时收益
     * @return
     *//*

    public TianJieWalletDto packageDtoSingle(TianJieWalletOld wallet, Boolean isCheckRevenue){
        BigDecimal currentRevenue = new BigDecimal("0");
        TianJieWalletDto dto = new TianJieWalletDto(wallet.getId(), wallet.getCreateTime(),wallet.getCreateDate(),
                wallet.getTotalAmount(),wallet.getUnreleaseAmount(),wallet.getReleaseAmount(),wallet.getAbleReleaseAmount(),wallet.getFrozenAmount(),currentRevenue);
        return dto;

    }

    */
/**
     * 组装交易记录DTO
     * @return
     *//*

    public List<TianJieWalletRecordDto> packageRecordDtoMuilt(List<TianJieWalletRecord> records){
        List<TianJieWalletRecordDto> dtos = new ArrayList<>();
        for(TianJieWalletRecord record:records){
            dtos.add(new TianJieWalletRecordDto(record.getId(), record.getCreateTime(),record.getCreateDate(),
                     record.getType(),record.getRemark(),record.getAmount()));
        }
        return dtos;
    }
    */
/**
     * 组装充值产品DTO
     * @return
     *//*

    public List<TianJieWalletProductDto> packageProductDtoMuilt(List<TianJieWalletProduct> products){
        List<TianJieWalletProductDto> dtos = new ArrayList<>();
        for(TianJieWalletProduct product:products){
            dtos.add(new TianJieWalletProductDto(product.getId(), product.getCreateTime(),product.getCreateDate(),
                    product.getAmount(), product.getType()));
        }
        return dtos;
    }
}
*/
